#!/bin/Rscript
library("ggplot2")
library("reshape")

#Avaliacao de reservas
#op_100_10 <- read.table("op_100_10/plans.dat")
#op_100_10 <- read.table("op_partial_100_10/plans.dat")
op_100_20 <- read.table("rf/plans.dat")
ut_100_20 <- read.table("ut/plans.dat")

#postscript(file="reservas_100_5.eps", paper="special",width=10,height=10, horizontal=FALSE)
jpeg("reservas_100_5.jpg")

df <- structure(c(mean(op_100_20$V1), mean(ut_100_20$V1), mean(op_100_20$V2*2), mean(ut_100_20$V2*2), mean(op_100_20$V3*2), mean(ut_100_20$V3*2), mean(op_100_20$V4*4), mean(ut_100_20$V4*4)), .Dim = c(2,
     4), .Dimnames = list(c("RF", "UT"), c("small", "large", "medium",
     "xlarge")))
df.m <- melt(df)
df.m <- rename(df.m, c(X1 = "Heur", X2 = "Tipo"))
p1 <- ggplot(df.m, aes(x = Heur, y = value,
     fill = Tipo)) + opts(title = "Risco de negação de serviço no IaaS: 5%") +
     labs(x = "Heurísticas Utilizadas", y = "Total de núcleos reservados") 
p1 + geom_bar(stat = "identity", position = "stack") +  scale_fill_grey() 

dev.off()


#>>>>>>>>>> Analise de consumo de horas 100 usuarios
#postscript(file="consumo_100.eps", paper="special",width=10,height=10, horizontal=FALSE)
jpeg("consumo_100.jpg")
par(mfrow=c(3, 3))

reserved <- c(1:70)
ondemand <- c(1:70)

#op_100_10 <- read.table("op_100_10/consumption.dat")
#op_100_10 <- read.table("op_partial_100_10/consumption.dat")
op_100_20 <- read.table("rf/consumption.dat")
#Total reserved hrs
initial <- 1
end <- 12
for (i in 1:70) {
	reserved[i] <- sum(as.numeric(op_100_20[initial:end, 1120]+op_100_20[initial:end, 1125] + op_100_20[initial:end, 1130]))
	initial = initial + 12
	end = end + 12
}

#Total on-demand hrs
initial <- 1
end <- 12
for (i in 1:70) {
	ondemand[i] <- sum(as.numeric(op_100_20[initial:end, 1118]+op_100_20[initial:end, 1123] + op_100_20[initial:end, 1128]))
	initial = initial + 12
	end = end + 12
}

#dados <- data.frame(total=reserved+ondemand, reservado=reserved, sobdemanda=ondemand, scen=c(1:70))
#a <- ggplot(data=dados)
#a+geom_line(aes(y=total, x=scen, colour="Total")) + opts(title = "Consumo de horas") +
#     labs(x = "Cenários de Simulação", y = "Total de CPU-hr consumido")
#last_plot()+geom_line(aes(y=reservado, x=scen, colour="Reservado"))
#last_plot()+geom_line(aes(y=sobdemanda, x=scen, colour="Sob demanda"))
plot(reserved+ondemand, col="red", type="l", pch=1, xlab="Repetições", ylab="Total de horas consumidas", main="OP 100 us 10%", ylim=c(0, 350000))
lines(reserved, col="blue", type="l", pch=4)
lines(ondemand, col="black", type="l", pch=6)


ut_100_20 <- read.table("ut/consumption.dat")
#Total reserved hrs
initial <- 1
end <- 12
for (i in 1:70) {
	reserved[i] <- sum(as.numeric(ut_100_20[initial:end, 1120]+ut_100_20[initial:end, 1125] + ut_100_20[initial:end, 1130]))
	initial = initial + 12
	end = end + 12
}

#Total on-demand hrs
initial <- 1
end <- 12
for (i in 1:70) {
	ondemand[i] <- sum(as.numeric(ut_100_20[initial:end, 1118]+ut_100_20[initial:end, 1123] + ut_100_20[initial:end, 1128]))
	initial = initial + 12
	end = end + 12
}
plot(reserved+ondemand, col="red", type="l", pch=1, xlab="Repetições", ylab="Total de horas consumidas", main="UT 100 us 10%", ylim=c(0, 350000))
lines(reserved, col="blue", type="l", pch=4)
lines(ondemand, col="black", type="l", pch=6)


par(xpd=NA)
#legend(locator(1), c("Total", "Reservado", "Sob demanda"), pch=19, col= c("red", "blue", "black"))
legend(-40, 500000, c("Total", "Reservado", "Sob demanda"), pch=c(1, 4, 6), col=c("red", "blue", "black") )

dev.off()

